<template>
  <el-dialog
    :title="form.id ? '编辑部门' : '添加部门'"
    :visible="show"
    width="60%"
    @close="close"
  >
    <!-- 表单内容 -->
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- 需要双向绑定 -->
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <!-- <el-button @click="close">取消</el-button> -->
      <el-button @click="$emit('close-dialog', false)">取消</el-button>
      <el-button type="primary" @click="addDept">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    // 点击当前的数据
    currDept: {
      type: Object,
      default: () => ({})
    },
    // 所有获取的平面数据, 用来校验
    allDepts: {
      type: Array,
      default: () => []
    }
  },
  data () {
    // 校验编码
    const validateCode = (rules, value, callback) => {
      // 如果有重复
      let flag
      // 如果是编辑要去掉当前的编码
      if (this.form.id) {
        flag = this.allDepts.some(item => item.code === value && this.currDept.code !== value)
      } else {
        flag = this.allDepts.some(item => item.code === value)
      }
      if (flag) {
        callback(new Error('编码重复'))
      } else {
        callback()
      }
    }
    return {
      // 负责人
      peoples: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getPeoples()
  },
  methods: {
    // 获取部门详情
    async getDetail (id) {
      const detail = await getDepartDetail(id)
      // console.log(detail)
      // 回显
      this.form = detail
    },
    // 获取负责人数据
    async getPeoples () {
      try {
        const peoples = await getEmployeeSimple()
        // console.log(peoples)
        this.peoples = peoples
      } catch (e) {
        console.log(e)
      }
    },
    // 点击取消
    close () {
      this.$emit('close-dialog', false)
      // 重置表单
      this.$refs.form.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    // 点击确定
    async addDept () {
      // 整体校验
      this.$refs.form.validate()
      try {
        if (this.form.id) {
          await updateDepartments(this.form)
        } else {
          await addDepartments({ ...this.form, pid: this.currDept.id || '' })
        }
        // pid: 点击的数据的父的数据,pid是子的pid, 所以用父的id就是子的pid
        this.$message.success('操作成功')
        // 刷新列表
        this.$emit('update-list')
        this.$emit('close-dialog', false)
        // this.$nextTick(() => {
        //   this.$refs.form.reset()
        // })
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>

<style lang="sass" scoped>
</style>
